<?php

class ViewHelper_Menu {
	
	/**
	 * @var Zend_Translate_Adapter
	*/
	protected $_translator;
	/**
	 * 翻译
	 * @param string $string
	 * @param string $type
	 * @return string
	*/
	protected function _($string,$options = null){
		$msg = $this->_translator->_($string);
		if(!empty($options) and is_array($options)){
			foreach ($options as $key=>$value){
				$msg = str_replace ( '{'.$key.'}', $value, $msg );
			}
		}
		return $msg;
	}	
	/*<ul>
	<li><a>
			<?php
			echo $this->translate ( 'menu.settings.manage' );
			?>
	<!--[if IE 7]><!--></a><!--<![endif]--> 
	<!--[if lte IE 6]><table><tr><td><![endif]-->
	<ul>
		<li><a
			href="<?php
			echo $this->pageInfo->getBaseUrl ();
			?>/<?php
			echo $this->pageInfo->getModuleUrl ();
			?>setting/list">
			<?php
			echo $this->translate ( 'menu.settings.list' );
			?>
		</a></li>
		<li><a
			href="<?php
			echo $this->pageInfo->getBaseUrl ();
			?>/<?php
			echo $this->pageInfo->getModuleUrl ();
			?>setting/new">
			<?php
			echo $this->translate ( 'menu.settings.new' );
			?>
		</a></li>
	</ul>
	<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>
	</ul>*/
	/*settings_manage.title = "menu.settings.manage"
	settings_manage.url = "#"
	settings_manage.notes.settings_list.title = "menu.settings.list"
	settings_manage.notes.settings_list.url = "setting/list"
	settings_manage.notes.settings_new.title = "menu.settings.new"
	settings_manage.notes.settings_new.url = "setting/new"
	
	*/
	/**
	 * 
	 * @param string $filename
	 * @param Object_PageInfo $pageInfo
	 * @return string
	*/
	public function menu($filename,$pageInfo){
		if(empty($this->_translator)){
			$this->_translator = Zend_Registry::get("Zend_Translate");
		}
		$url_extend = $pageInfo->getCurrentModuleUrl();
		$savename = str_replace('.','_',$filename);
		$filename = CONFIG_DIR.DIRECTORY_SEPARATOR.'menu'.DIRECTORY_SEPARATOR.$filename;
		
		$fileObject = Config_Xml::readIni($filename,$savename);
		$menuArray = $fileObject->toArray();		
		$html = "<div id='".$savename."'>";
		foreach ($menuArray as $id=>$menuValue){
			$html .= "<ul id='".$id."'>";
			$html .= '<li>';
			$html .= '<a ';			
			$html .= $this->_getHref($menuValue,$url_extend);
			$html .= '>';
			$html .= $this->_($this->_getText($menuValue));
			$html .= '<!--[if IE 7]><!--></a><!--<![endif]-->';
			if(isset($menuValue['notes'])){
				$html .="<ul>";
				$html .="<!--[if lte IE 6]><table><tr><td><![endif]-->";
				$notesValue = $menuValue['notes'];				
				foreach ($notesValue as $id=>$noteValue){
					$html .= "<li id='".$id."'>";
					$html .= "<a ".$this->_getHref($noteValue,$url_extend).">";
					$html .= $this->_($this->_getText($noteValue));
					$html .= '</a>';
					$html .= '</li>';
				}
				$html .="</ul>";
				$html .="<!--[if lte IE 6]></td></tr></table></a><![endif]-->";
			}
			$html .="</li>";
			$html .='</ul>';
		}
		$html .= "</div>";
		return $html;
	}
	
	protected function _getText($menuValue){ 
		if(isset($menuValue['title'])){
			return $menuValue['title'];
		}
		$url = isset($menuValue['url'])?$menuValue['url']:'#';
		if($url == '#'){
			throw new Exception('set url text name');
		}
		$url = str_replace('/','.',$url);
		$url = 'menu.'.$url;
		return $url;
	}
	
	protected function _getHref($menuArray,$url_extend){
		$url = isset($menuArray['url'])?$menuArray['url']:'#';
		$html = "";
		if($url != '' and $url != '#'){
			$url = $url_extend.$url;
			$html .= "href='".$url."'";
		}		
		return $html;
	}
}